#!/bin/bash
clear

# INFO='\033[97;44m'
# WARNING='\033[97;43m'
# SUCCESS='\033[97;42m'
YELLOW='\033[93m'
GREEN='\033[92m'
TEXT='\033[30;107;2m'
ERROR='\033[97;41m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
	echo -e "${ERROR} ERROR ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
	exit 1
fi

checkClientName=$(grep -c -E "^### START" "/etc/wireguard/wg0.conf")
if [[ ${checkClientName} == '0' ]]; then
	echo "You have no existing clients!"
	exit 1
fi

echo
echo "Select the existing client you want to renew"
grep -E "^### START" "/etc/wireguard/wg0.conf" | cut -d ' ' -f 3 | nl -s ') '
until [[ ${ClientNum} -ge 1 && ${ClientNum} -le ${checkClientName} ]]; do
	if [[ ${ClientNum} == '1' ]]; then
		read -rp "Sila pilih pengguna [1]: " ClientNum
	else
		read -rp "Sila pilih pengguna [1-${checkClientName}]: " ClientNum
	fi
done

read -rp "Masukkan tempoh aktif [Hari]: " ActiveDuration

oldExpDate=$(grep -E "^### START" "/etc/wireguard/wg0.conf" | cut -d ' ' -f 4)
newExpDate=$(date -d "$oldExpDate" +"%F")
sed -i "s|### START $UserName $oldExpDate|### START $UserName $newExpDate|g" /etc/wireguard/wg0.conf
sed -i "s|### END $UserName $oldExpDate|### END $UserName $newExpDate|g" /etc/wireguard/wg0.conf

clear
echo
echo -e "${TEXT} ========================================================== ${RESET}"
echo -e "${TEXT} WIREGUARD - PERBAHARUI AKAUN PENGGUNA                      ${RESET}"
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo
echo -e "${YELLOW} Nama pengguna :${RESET} ${GREEN}$UserName${RESET}"
echo -e "${YELLOW} Tempoh aktif  :${RESET} ${GREEN}$ActiveDuration hari${RESET}"
echo -e "${YELLOW} Tarikh luput  :${RESET} ${GREEN}$newExpDate${RESET}"
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo
